package ch.il07.converter.presentation.beans;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import ch.il07.converter.business.ServiceFactoryAdapter;
import ch.il07.converter.business.UserBusiness;

public class ChangeUserDataBean {
	private String oldPassword;
	private String newPassword;
	private String newConfirmedPassword;
	private String newEmail;

	public ChangeUserDataBean() {
		
	}

	public String changeUserData() {
		String response = "failure";
		if (newPassword.equals(newConfirmedPassword)) {
			ExternalContext fc = FacesContext.getCurrentInstance().getExternalContext();
			String username = (String) ((HttpSession) fc.getSession(true)).getAttribute("username");

			UserBusiness userImpl = ServiceFactoryAdapter.getUserBusinessInstance();
			boolean result = userImpl.changePassword(username, oldPassword,	newPassword);
			response = result ? "success" : "failure";
		}
		return response;
	}

	public String cancel() {
		return "cancel";
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getNewConfirmedPassword() {
		return newConfirmedPassword;
	}

	public void setNewConfirmedPassword(String newConfirmedPassword) {
		this.newConfirmedPassword = newConfirmedPassword;
	}

	public String getNewEmail() {
		return newEmail;
	}

	public void setNewEmail(String newEmail) {
		this.newEmail = newEmail;
	}
}
